package cn.bx.bid.ctrl;

import cn.bx.bid.entity.AjaxResult;
import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.Project;
import cn.bx.bid.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class ProjectAjaxController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping("projectSearch")
    @ResponseBody
    public Page<Project> paging(@RequestParam(name = "pageNo",defaultValue = "1") int pageNo,
                                @RequestParam(name = "limit",defaultValue = "10") int limit,
                                @RequestParam(name = "pname",required = false) String name, @RequestParam(name = "category",required = false) String category) {
        Page<Project> p = this.projectService.paging(pageNo,limit,name,category);
        return p;
    }

    @RequestMapping("delProject")
    @ResponseBody
    public AjaxResult del(@RequestParam("pid")int id){
        return projectService.del(id) > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @RequestMapping("getProject")
    @ResponseBody
    public Project get(@RequestParam("pid") int id){
        return projectService.get(id);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
//        binder.registerCustomEditor(Date.class,"属性名称", new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值 }
    }
}
